package com.ruoyi.web.controller.server;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EmDeclaration;
import com.ruoyi.system.enums.DeclarationStateEnum;
import com.ruoyi.system.service.IEmDeclarationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 灾害申报Controller
 *
 * @author yangcan
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/system/declaration")
public class EmDeclarationController extends BaseController
{
    @Autowired
    private IEmDeclarationService emDeclarationService;

    /**
     * 查询灾害申报列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmDeclaration emDeclaration)
    {
        startPage();
        List<EmDeclaration> list = emDeclarationService.selectEmDeclarationList(emDeclaration);
        for (EmDeclaration declaration : list) {
            declaration.setStateDescribe(DeclarationStateEnum.getMessageByCode(declaration.getState()));
        }
        return getDataTable(list);
    }

    /**
     * 导出灾害申报列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmDeclaration emDeclaration)
    {
        List<EmDeclaration> list = emDeclarationService.selectEmDeclarationList(emDeclaration);
        ExcelUtil<EmDeclaration> util = new ExcelUtil<EmDeclaration>(EmDeclaration.class);
        util.exportExcel(response, list, "灾害申报数据");
    }

    /**
     * 获取灾害申报详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emDeclarationService.selectEmDeclarationById(id));
    }

    /**
     * 新增灾害申报
     */
    @PostMapping
    public AjaxResult add(@RequestBody EmDeclaration emDeclaration)
    {
        return toAjax(emDeclarationService.insertEmDeclaration(emDeclaration));
    }

    /**
     * 修改灾害申报
     */
    @PutMapping
    public AjaxResult edit(@RequestBody EmDeclaration emDeclaration)
    {
        return toAjax(emDeclarationService.updateEmDeclaration(emDeclaration));
    }

    /**
     * 删除灾害申报
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emDeclarationService.deleteEmDeclarationByIds(ids));
    }

    /**
     * 采纳灾害申报
     */
    @GetMapping("/acceptDeclaration")
    public AjaxResult acceptDeclaration(@RequestParam Long id)
    {
        emDeclarationService.acceptDeclaration(id) ;
        return success();
    }
}
